//
//  ChartSubView.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 07.04.25.
//

import SwiftUI

struct ChartSubView: View {
    
    @State var date: Date
    @State var category: AccountingCategory
    @StateObject var viewModel = ChartViewModel()
    
    init(date: Date, category: AccountingCategory, dateSelectType: DateSelectType) {
        self.date = date
        self.category = category
        _viewModel = StateObject(wrappedValue: ChartViewModel(
            dateSelectType: dateSelectType,
            categoryType: category.getCategoryType()
        ))
    }
    
    var body: some View {
        VStack {
            ZStack {
                HStack {
                    KoiBackButton()
                    Text(category.name.orEmpty)
                        .font(.system(size: 14))
                        .foregroundColor(Color.koiColorTitle)
                    Spacer()
                }
                HStack {
                    KoiTabBar<DateSelectType>(selection: $viewModel.dateSelectType, items: [
                        TabBarItem(tab: DateSelectType.week, name: DateSelectType.week.typeName()),
                        TabBarItem(tab: DateSelectType.month, name: DateSelectType.month.typeName()),
                        TabBarItem(tab: DateSelectType.year, name: DateSelectType.year.typeName()),
                    ]).padding(.horizontal, 20)
                }
            }
            TabView(selection: $viewModel.dateSelectType) {
                ChartDateWeekView(
                    date: $date,
                    categoryType: $viewModel.categoryType,
                    parentCategory: category).tag(DateSelectType.week)
                ChartDateMonthView(
                    date: $date,
                    categoryType: $viewModel.categoryType,
                    parentCategory: category).tag(DateSelectType.month)
                ChartDateYearView(
                    date: $date,
                    categoryType: $viewModel.categoryType,
                    parentCategory: category).tag(DateSelectType.year)
            }.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
        }
        .navigationBarBackButtonHidden(true)
        .background(Color.koiColorBackground)
        .onAppear {
            print("ChartSubView onAppear date \(date)")
        }
    }
}

#Preview {
    ChartSubView(date: Date(), category: AccountingCategory(),  dateSelectType: .week)
}
